<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
      $list_special_product = array();
      $list_new_product = array();
      $model = new Books();
      
      $criteria = new CDbCriteria();
      $criteria->addCondition('book_special = 1');
      $criteria->limit = 6;
      $criteria->order = 'book_id DESC';
      
      $list_special_product = $model->with('publisher')->findAll($criteria);
      
      $criteria = new CDbCriteria();
      $criteria->limit = 6;
      $criteria->order = 'book_id DESC';
      $criteria->addCondition('book_sale = 0');
      $list_new_product = $model->with('publisher')->findAll($criteria);
      
      $criteria = new CDbCriteria();
      $criteria->limit = 6;
      $criteria->addCondition('book_sale > 0');
      $criteria->order = 'book_id DESC';
      $list_sale_product = $model->with('publisher')->findAll($criteria);
      
      $criteria = new CDbCriteria();
      $criteria->limit = 6;
      $criteria->addCondition('book_date_publish > ' . time());
      $criteria->order = 'book_date_publish ASC';
      $list_publish_product = $model->with('publisher')->findAll($criteria);
      
      $criteria = new CDbCriteria();
      $criteria->order = 'book_sale DESC';
      $criteria->limit = 1;
      $max_of_discount = $model->find($criteria);
      //CVarDumper::dump($max_of_discount,10,true);die();
		$this->render('index',array(
         'model' => $model,
         'list_special_product' => $list_special_product,
         'list_new_product' => $list_new_product,
         'list_publish_product' => $list_publish_product,
         'list_sale_product' => $list_sale_product,
         'max_of_discount' => $max_of_discount,
      ));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model = new Feedback();
      if(isset($_POST['Feedback'])){
         $model->attributes = $_POST['Feedback'];
         $model->feed_date_create = time();
         if($model->validate()){
            if($model->save()){
               Yii::app()->user->setFlash('success','Cảm ơn! Chúng tôi đã nhận được ý kiến phản hồi của bạn.');
               $model->unsetAttributes();
            }
         }
      }
		// display the login form
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		if(Yii::app()->session['user_id'])
         $this->redirect('/');
      $model=new LoginForm;
      
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
         {
            Yii::app()->session['user_id'] = Yii::app()->user->id;
            $this->redirect(Yii::app()->user->returnUrl);
         }
				
		}
      
		// display the login form
      $this->layout = 'shop';
		$this->render('login',array('model'=>$model));
	}
   
   public function actionRegister(){
      
      $model=new Member;
      
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['Member']))
		{
			$model->attributes=$_POST['Member'];
         $model->MemberGroupID = 4; // User đăng ký thuộc nhóm người dùng
         if($model->validate()){
            $model->Password = md5($model->Password);
            if($model->save()){
               Yii::app()->user->setFlash('success','Đăng ký thành công');
               $model->unsetAttributes();
            }
               
         }
         $model->unsetAttributes(array(
            'Password',
            'repeatPassword',
         ));
         
			
		}
      
      $this->render('register',array('model'=>$model));
      
      
   }

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}